# Contributor: Yichun Zhang <admin@openresty.com>
# Maintainer: Yichun Zhang <admin@openresty.com>
pkgname="openresty-pcre"
pkgver="8.45"
pkgrel=0
pkgdesc="Perl-compatible regular expression library for OpenResty"
url="http://www.pcre.org/"
arch="all"
license="BSD"
#depends=""
makedepends="ccache"
subpackages="$pkgname-static $pkgname-dev $pkgname-doc $pkgname-dbg"
source="https://sourceforge.net/projects/pcre/files/pcre/$pkgver/pcre-$pkgver.tar.bz2"
builddir="$srcdir/pcre-$pkgver/"
options="!tracedeps !fhs"

_pcre_prefix=/usr/local/openresty/pcre

build() {
    export CC="ccache gcc -fdiagnostics-color=always -g3"
    export CFLAGS=
    export CXXFLAGS=
    export CPPFLAGS=
    export LDFLAGS=

    ./configure \
      --disable-dependency-tracking \
      --prefix=$_pcre_prefix \
      --libdir=$_pcre_prefix/lib \
      --disable-cpp \
      --enable-jit \
      --enable-utf \
      --enable-unicode-properties

    make -j$JOBS
}

check() {
	:
}

package() {
    make install DESTDIR="$pkgdir"
    rm -rf $pkgdir$_pcre_prefix/bin
    rm -f  $pkgdir$_pcre_prefix/lib/*pcrecpp*
    rm -f  $pkgdir$_pcre_prefix/lib/*pcreposix*
}

doc() {
    local gzip=$(command -v pigz || echo gzip)
    depends="$depends_doc"
    pkgdesc="$pkgdesc (documentation)"
    install_if="docs $pkgname=$pkgver-r$pkgrel"

    local i
    for i in doc man info html sgml licenses gtk-doc ri help; do
        if [ -d "$pkgdir$_pcre_prefix/share/$i" ]; then
            mkdir -p "$subpkgdir$_pcre_prefix/share"
            mv "$pkgdir$_pcre_prefix/share/$i" "$subpkgdir$_pcre_prefix/share/"
        fi
    done

    # compress man pages
    local mandir="$subpkgdir"$_pcre_prefix/share/man
    [ -d "$mandir" ] && find "$mandir" -type l \
        -a \( -name \*.[0-8n] -o -name \*.[0-8][a-z]* \) \
        -a \! \( -name '*.gz' -o -name '*.bz2' -o -name '*.xz' \) \
        | while read symlink; do

        ln -s $(readlink $symlink).gz "$symlink".gz
        rm -f "$symlink"
    done
    [ -d "$mandir" ] && find "$mandir" -type f \
        -a \( -name \*.[0-8n] -o -name \*.[0-8][a-z]* \) \
        -a \! \( -name '*.gz' -o -name '*.bz2' -o -name '*.xz' \) \
        -exec stat -c "%i %n" \{\} \+ | while read inode name; do

        # Skip hardlinks removed in last iteration.
        [ -f "$name" ] || continue

        local islink=0
        find "$mandir" -type f -links +1 \
            -a \( -name \*.[0-8n] -o -name \*.[0-8][a-z]* \) \
            -a \! \( -name '*.gz' -o -name '*.bz2' -o -name '*.xz' \) \
            -exec stat -c "%i %n" \{\} \+ | while read linode lname; do
            if [ "$linode" = "$inode" -a "$lname" != "$name" ]; then
                islink=1
                rm -f "$lname"
                ln -s "${name##*/}".gz "$lname".gz
            fi
        done

        [ $islink -eq 0 ] && $gzip -n -9 "$name"
    done

    rm -f "$subpkgdir$_pcre_prefix/share/info/dir"

    # remove if empty, ignore error (not empty)
    rmdir "$pkgdir$_pcre_prefix/share" "$pkgdir$_pcre_prefix" 2>/dev/null || :
}

static() {
    if ! is_function default_static; then
        local i= devpkg

        # search for -dev package matching our prefix
        if [ -z "$depends_static" ]; then
            devpkg="${subpkgname%-libs-static}"
            devpkg="${devpkg%-static}"
            devpkg="$devpkg-dev"
            if subpackages_has "$devpkg"; then
                depends_static="$devpkg"
            fi
        fi

        depends="$depends_static"
        pkgdesc="$pkgdesc (static library)"

        cd "$pkgdir" || return 0
        local libdirs=usr/lib
        [ -d lib/ ] && libdirs="lib/ $libdirs"

        # move *.a static library
        for i in $(find $libdir -name '*.a'); do
            mkdir -p "$subpkgdir"/"${i%/*}"
            mv "$i" "$subpkgdir/$i" || return 1
        done
        return 0
    fi

    default_static
}

sha512sums="
91bff52eed4a2dfc3f3bfdc9c672b88e7e2ffcf3c4b121540af8a4ae8c1ce05178430aa6b8000658b9bb7b4252239357250890e20ceb84b79cdfcde05154061a  pcre-8.45.tar.bz2
"
